"use strict";
cc._RF.push(module, '639far1QFRNz4plSA1jRfo3', 'AudioUtil');
// libs/lightMVC/util/AudioUtil.ts

"use strict";
/**
 * 音频工具类
 * @author ituuz
 * @description 主要负责音乐、音效的播放、暂停和停止逻辑，包括缓存。
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MusicType = void 0;
var ITResourceLoader_1 = require("../loader/ITResourceLoader");
/** 音频类型枚举 */
var MusicType;
(function (MusicType) {
    MusicType[MusicType["SOUND"] = 0] = "SOUND";
    MusicType[MusicType["MUSIC"] = 1] = "MUSIC";
    MusicType[MusicType["LOOP_SOUND"] = 2] = "LOOP_SOUND"; // 循环音效
})(MusicType = exports.MusicType || (exports.MusicType = {}));
/** 音频工具类 */
var AudioUtil = /** @class */ (function () {
    function AudioUtil() {
    }
    /**
     * 播放音效
     * @param {string} path 音效资源
     */
    AudioUtil.playEffect = function (path) {
        if (AudioUtil._pauseFlag) {
            return;
        }
        var curTime = new Date().getTime();
        if (path === AudioUtil._curSound) {
            var tempTime = curTime - AudioUtil._lastSoundTime;
            if (tempTime < 50) {
                return;
            }
        }
        AudioUtil._curSound = path;
        AudioUtil._lastSoundTime = curTime;
        var source = AudioUtil.getEffectFromPool(path);
        if (source) {
            source.play(false);
        }
        else {
            source = new __AudioSource(path, MusicType.SOUND);
            AudioUtil.addEffectToPool(path, source);
            source.play(false);
        }
    };
    /** 暂停音效 */
    AudioUtil.stopEffect = function () {
        AudioUtil._soundPool.forEach(function (value, key, map) {
            if (value) {
                for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
                    var source = value_1[_i];
                    source.stop();
                }
            }
        });
    };
    /** 播放循环音效 */
    AudioUtil.playLoopEffect = function (path) {
        if (AudioUtil._pauseFlag) {
            return;
        }
        var source = AudioUtil._loopSoundPool.get(path);
        if (!source) {
            source = new __AudioSource(path, MusicType.LOOP_SOUND);
            AudioUtil._loopSoundPool.set(path, source);
            source.play(true);
        }
    };
    /** 暂停循环音效 */
    AudioUtil.stopLoopEffect = function (path) {
        var source = AudioUtil._loopSoundPool.get(path);
        if (source) {
            source.destroy();
            AudioUtil._loopSoundPool.delete(path);
        }
    };
    /**
     * 播放音效
     * @param {string} path 播放背景音乐
     */
    AudioUtil.playMusic = function (path) {
        if (AudioUtil._pauseFlag) {
            return;
        }
        if (AudioUtil._curMusic) {
            AudioUtil._curMusic.stop();
        }
        var source = AudioUtil._musicPool.get(path);
        if (source) {
            source.play(true);
        }
        else {
            source = new __AudioSource(path, MusicType.MUSIC);
            AudioUtil._musicPool.set(path, source);
            source.play(true);
        }
        AudioUtil._curMusic = source;
    };
    /** 暂停音效 */
    AudioUtil.stopMusic = function () {
        if (AudioUtil._curMusic) {
            AudioUtil._curMusic.stop();
        }
    };
    /** 从音效对象池获取对象 */
    AudioUtil.getEffectFromPool = function (path) {
        var list = AudioUtil._soundPool.get(path);
        if (list) {
            for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
                var source = list_1[_i];
                if (!source.isPlaying()) {
                    return source;
                }
            }
        }
        return null;
    };
    /** 像音效对象池添加对象 */
    AudioUtil.addEffectToPool = function (path, source) {
        var list = AudioUtil._soundPool.get(path);
        if (list) {
            list.push(source);
        }
        else {
            list = [];
            list.push(source);
            AudioUtil._soundPool.set(path, list);
        }
    };
    /**
     * 设置暂停所有音效标志
     * @param {boolean} flag 标志 true-暂停  false-恢复
     * @param {number} level 设置状态级别，数字越大级别越高，高级别逻辑覆盖低级别，低级别不能覆盖高级别。
     */
    AudioUtil.setPauseFlag = function (flag, level) {
        if (level === void 0) { level = 0; }
        AudioUtil._level = level;
        AudioUtil._pauseFlag = flag;
        if (flag) {
            if (AudioUtil._curMusic) {
                AudioUtil._curMusic.pause();
            }
            AudioUtil.pauseLoopEffects();
        }
        else {
            if (AudioUtil._curMusic) {
                AudioUtil._curMusic.resume();
            }
            AudioUtil.resumeLoopEffects();
        }
    };
    /** 暂停所有循环音效 */
    AudioUtil.pauseLoopEffects = function () {
        AudioUtil._loopSoundPool.forEach(function (v, key) {
            v.pause();
        });
    };
    /** 恢复所有循环音效 */
    AudioUtil.resumeLoopEffects = function () {
        AudioUtil._loopSoundPool.forEach(function (v, key) {
            v.resume();
        });
    };
    /** 背景音乐缓存池 */
    AudioUtil._musicPool = new Map();
    /** 音效缓存池 */
    AudioUtil._soundPool = new Map();
    /** 循环音效 */
    AudioUtil._loopSoundPool = new Map();
    /** 当前音效 */
    AudioUtil._curSound = "";
    /** 上次播放音效时间 */
    AudioUtil._lastSoundTime = 0;
    /** 是否暂停所有音效 */
    AudioUtil._pauseFlag = false;
    /** 当前音频状态级别，数字越大越高 配合_pauseFlag判断 */
    AudioUtil._level = 0;
    return AudioUtil;
}());
exports.default = AudioUtil;
// tslint:disable-next-line: class-name
var __AudioSource = /** @class */ (function () {
    function __AudioSource(path, type) {
        // 是否加载完成
        this._loaded = false;
        // 音频资源
        this._audioSource = new cc.AudioSource();
        // 音频路径
        this._path = "";
        this._path = path;
        this._musicType = type;
    }
    /**
     * 音频加载接口
     * @param {() => void} cb 加载完成回调
     */
    __AudioSource.prototype.loadRes = function (cb) {
        var _this = this;
        if (!this._loaded) {
            ITResourceLoader_1.default.loadRes(this._path, cc.AudioClip, function (err, res) {
                if (err) {
                    //it.warn("音频资源加载出错:", this._path);
                }
                else {
                    _this._audioSource.clip = res;
                    _this._loaded = true;
                    if (cb) {
                        cb();
                    }
                }
            });
        }
    };
    __AudioSource.prototype.play = function (isLoop) {
        var _this = this;
        this._isStop = false;
        if (this._loaded) {
            this._audioSource.loop = isLoop;
            this._audioSource.play();
        }
        else {
            this.loadRes(function () {
                if (!_this._isStop) {
                    _this.play(isLoop);
                }
            });
        }
    };
    __AudioSource.prototype.isPlaying = function () {
        return this._audioSource.isPlaying;
    };
    __AudioSource.prototype.stop = function () {
        this._isStop = true;
        if (this._loaded) {
            this._audioSource.stop();
        }
    };
    __AudioSource.prototype.pause = function () {
        if (this._loaded) {
            this._audioSource.pause();
        }
    };
    __AudioSource.prototype.resume = function () {
        if (this._loaded) {
            this._audioSource.resume();
        }
    };
    __AudioSource.prototype.destroy = function () {
        this._audioSource.stop();
        this._audioSource = null;
    };
    return __AudioSource;
}());
// 将接口导出
// tslint:disable-next-line: no-unused-expression
window.it || (window.it = {});
window.it.AudioUtil = AudioUtil;

cc._RF.pop();